export default {
    data() {
        return {
            mixinTable_tableDataName:"mixinTable_tableData",
            mixinTable_tableData:[],
            // 列表动态查询参数
            mixinTable_tableSearchData: {},
            // 列表常量查询参数
            mixinTable_tableSearchConst: {},
            // 列表高级查询参数
            mixinTable_tableSearchSenior: {},
            // 列表加载状态
            mixinTable_loading:true,
            //   分页
            mixinTable_pageData: {
                // 当前页
                pageNum: 0,
                // 每页显示条数
                pageSize: this.$vFN.Constants.pageSize,
                // 总条数
                total: 0,
                // 总页数
                totalPage: 0,
            },
            // 选择表格的数据 用于对表格的批量操作
            mixinTable_selectTableData: [],
            // 表格数据类型  list | tree
            mixinTable_tableDataType: "list",
        }
    },
    methods: {
        // 选择表格时
        mixinTable_handleSelectionChangeTableSmall(val_) {
            this.mixinTable_selectTableData = val_;
        },
        // 取消选择
        mixinTable_clearSelectionChangeTableSmall() {
            this.$refs[this.mixinPage_vueName + 'TableSmall'].clearSelection();
        },
        /**
         * 删除表格数据
         * @param {*} ids 数组 id
         */
        async mixinTable_selectTableDeleteData(data_) {
            if (!data_) {
                data_ = [...this.mixinTable_selectTableData]
            }
            // console.error("this.$vFN",this.$vFN.Directives.)
            this.$vFN.Directives.TableDeleteBox({
                appWindow:this.appWindow,
                data: data_,
                config: this.tableDeleteConfig,
                url: this.apiUrl.delete,
                onSuccess: () => {
                    this.$vFN.Tools.messageSuccess("删除成功");
                    this.mixinTable_refreshData();
                },
            })
        },

        async mixinTable_getTableValidate(data_) {
            // console.error("mixinTable_getTableValidate",data_)
            return true
        },
        /**
         * 获取表格数据列表

         * @param {*} pageNum_ 选填，不传时为刷新本页，
         */
        async mixinTable_getTable() {
        
            // if (!pageNum_) {
            //     pageNum_ = this.mixinTable_pageData.pageNum;
            // }
            this.mixinTable_loading = true;
            let _pageSize = this.mixinTable_pageData.pageSize;
            let _d = {
                // delayTime: 1000,
                data: {
                    ...this.mixinTable_tableSearchData,
                    // ...this.mixinTable_tableSearchSenior,
                    ...this.mixinTable_tableSearchConst
                },
            }

            if(typeof this.apiUrl.list=="object"){
                _d.url = this.apiUrl.list.url;
                _d.domain = this.apiUrl.list.domain;
            }
            else{
                _d.url = this.apiUrl.list;
            }
            for (let i in this.mixinTable_tableSearchSenior) {
                let _item = this.mixinTable_tableSearchSenior[i];
                let _value = _item.value;
                // 为空数组时不为null 则设置为null 后台传参要求
                if (_item.type == Array && _value != null) {
                    if (_value.length <= 0) {
                        _value = null
                    }
                }
                _d.data[i] = _value
            }
            // 表格数据格式返回的数据格式不同 list 支持分页 ，tree不支持分页
            if (this.mixinTable_tableDataType == "list") {
                _d.params = {
                    pageNum: this.mixinTable_pageData.pageNum,
                    pageSize: _pageSize,
                }
            }

            let _is = await this.mixinTable_getTableValidate(_d);
            if(!_is){
                this.mixinTable_loading = false;
                return
            }
            let _data = await this.$vFN.Http.post(_d,this.appWindow);
            if (_data._ok) {
                // 表格数据格式返回的数据格式不同 list 支持分页 ，tree不支持分页
                if (this.mixinTable_tableDataType == "list") {
                    this[this.mixinTable_tableDataName] = this.mixinTable_getTableExtend(_data.data.list);
                    this.mixinTable_setPageData(_data.data);
                } else if (this.mixinTable_tableDataType == "tree") {
                    this[this.mixinTable_tableDataName] = this.mixinTable_getTableExtend(_data.data);
                }

                this.mixinTable_loading = false;
            }
        },
        // 扩展方法，
        mixinTable_getTableExtend(data_) {
            return data_
        },
        // 设置分页数据
        mixinTable_setPageData(data_) {
            this.mixinTable_pageData.pageNum = data_.pageNum-0;
            this.mixinTable_pageData.total = data_.total-0;
            this.mixinTable_pageData.totalPage = data_.totalPage-0;

        },
        // 查询
        mixinTable_searchData() {
            this.mixinTable_pagingData(1)
            return
            if(this.mixinTable_pageData.pageNum==1){
                this.mixinTable_getTable();
                console.error(1);
            }
            else{
                console.error(2);
                this.mixinTable_pagingData(1)
            }
        },
        // 分页跳转
        mixinTable_pagingData(e) {
            let _query = JSON.parse(JSON.stringify(this.$route.query));
            // console.error(222, {
            //     ..._query
            // });
            // 如果是站点管理则添加站点id
            if (this.$vFN.Tools.getProjectType() == 'site') {
                _query.sid = this.$vFN.Tools.getSid();
            }
            // 列表动态查询参数
            for (let i in this.mixinTable_tableSearchData) {
                if (this.mixinTable_tableSearchData[i]) {
                    _query[i] = this.mixinTable_tableSearchData[i];
                } else {
                    delete _query[i]
                }
            }

            // 列表高级查询参数
            for (let i in this.mixinTable_tableSearchSenior) {
                let _item = this.mixinTable_tableSearchSenior[i]
                // console.error(_item);
                if (_item.value != _item.default && !this.$vFN.Utils.isEmpty(_item.value)) {
                    if (_item.type == Array) {
                        _query[i] = _item.value.join(",");
                    } else {
                        _query[i] = _item.value;
                    }
                } else {
                    delete _query[i]
                }
            }
           
            _query.pageNum = e;
            
            this.$router.push({
                query: _query
            });


        },


        // 刷新页面
        mixinTable_initSearchData() {
            let _query = {
                ...this.$route.query
            };
            // 列表动态查询参数
            for (let i in this.mixinTable_tableSearchData) {
                if (_query[i]) {
                    this.mixinTable_tableSearchData[i] = _query[i];
                } else {
                    this.mixinTable_tableSearchData[i] = "";
                }
            }
            // 列表高级查询参数

            for (let i in this.mixinTable_tableSearchSenior) {
                if (_query[i]) {
                    let _item = this.mixinTable_tableSearchSenior[i];
                    let _value;
                    if (_item.type == Number) {
                        _value = _query[i] - 0;
                    } else if (_item.type == Array) {
                        _value = _query[i].split(",");
                    } else if (_item.type == Object) {
                        _value = JSON.parse(_query[i]);
                    } else {
                        _value = _query[i];
                    }
                    this.mixinTable_tableSearchSenior[i].value = _value;

                }
            }
            this.mixinTable_pageData.pageNum = _query.pageNum ? _query.pageNum - 0 : 1;
            // this.mixinTable_pageData.pageSize = _query.pageSize ? _query.pageSize - 0 : this.$vFN.Constants.pageSize;

            // 获取表格数据
            this.mixinTable_getTable();
        },


        // 高级搜索重置
        mixinTable_searchSeniorReset() {

            for (let i in this.mixinTable_tableSearchSenior) {
                this.mixinTable_tableSearchSenior[i].value = this.mixinTable_tableSearchSenior[i].default;
            }
            for (let i in this.mixinTable_tableSearchData) {
                this.mixinTable_tableSearchData[i] = "";
            }
            this.mixinTable_searchData()
        },
        // 更新页面回调
        mixinTable_refreshData() {
            this.mixinTable_initSearchData();
        },
        //自定义索引
        mixinTable_indexMethod(index) {
            return (
                index +
                1 +
                (this.mixinTable_pageData.pageNum - 1) * this.mixinTable_pageData.pageSize
            );
        },


    },
    mounted() {
        let that = this;
        this.$nextTick(() => {});

    },
    watch: {
        $route: {
            handler() {
                this.mixinTable_refreshData();
            },
            deep: true,
        },
    },
}